package com.vitalfit.controller;

import com.vitalfit.enums.ResultEnum;
import com.vitalfit.exception.UserNotFoundException;
import com.vitalfit.mapper.UserMapper;
import com.vitalfit.model.dto.HandleDTO;
import com.vitalfit.model.entity.user.Customer;
import com.vitalfit.model.entity.user.Trainer;
import com.vitalfit.model.entity.user.User;
import com.vitalfit.model.vo.ResultVO;
import com.vitalfit.model.vo.UserVO;
import com.vitalfit.repository.UserRepository;
import com.vitalfit.service.serviceImpl.AuthServiceImpl;
import com.vitalfit.strategy.UserStrategy;
import com.vitalfit.strategy.factory.UserStrategyFactory;
import com.vitalfit.utils.ResultVOUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserRepository userRepository;

    private final UserStrategyFactory userStrategyFactory;

    private final AuthServiceImpl authServiceImpl;



    @GetMapping("/customer/{userId}")
    public Customer getCustomerById(@PathVariable Integer userId) {
        return (Customer)userRepository.findById(userId)
                .orElseThrow(() -> new UserNotFoundException("User not found with id: " + userId));
    }

    @GetMapping("/trainer/{trainerId}")
    public Trainer getTrainerById(@PathVariable Integer trainerId) {
        return (Trainer) userRepository.findById(trainerId)
                .orElseThrow(() -> new UserNotFoundException("User not found with id: " + trainerId));
    }

    @PostMapping("/trainer")
    public Trainer getTrainerByEmail(@RequestBody String trainerEmail) {
        return (Trainer) userRepository.findByEmail(trainerEmail);
    }


    @GetMapping("/{userId}")
    public UserVO getUserById(@PathVariable Integer userId) {
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new UserNotFoundException("User not found with id: " + userId));
        return UserMapper.INSTANCE.userToUserVO(user);

    }

    @PutMapping
    public ResultVO<Object> updateUserInfo(@RequestBody HandleDTO handleDTO) {
        UserStrategy strategy = userStrategyFactory.getStrategy("Customer", "edit");
        return ResultVOUtil.buildResult(ResultEnum.LOGIN_SUCCESS,strategy.execute(handleDTO));
    }
    @PostMapping("/query/trainer")
    public ResultVO<Object> getTrainer(@RequestBody HandleDTO handleDTO) {
        UserStrategy strategy = userStrategyFactory.getStrategy("Trainer", "query");
        return ResultVOUtil.buildResult(ResultEnum.LOGIN_SUCCESS,strategy.execute(handleDTO));
    }

    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @PostMapping
    public ResultVO<List<User>> getAllUser(@RequestParam(defaultValue = "0") int page,
                                     @RequestParam(defaultValue = "10") int size) {

        return ResultVOUtil.buildResult(ResultEnum.LOGIN_SUCCESS,authServiceImpl.getAllUser(page,size));
    }

}

